package com.taoyibao.hongcanting.ui.mine.delegate;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author TangAnna
 * @description: 修改昵称
 * @date :${DATA} 13:52
 */
public class ChangeNickDelegate extends BackToolbarDelegate {
    private EditText mEtNick;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_change_nick;
    }


    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("修改昵称");
        mEtNick = get(R.id.et_changeNick_input);
        mEtNick.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String input = mEtNick.getText().toString().trim();
                if (!TextUtils.isEmpty(input)) {
                    String nickMatcher = "^[\\u4e00-\\u9fa5a-zA-Z]+$";//正则匹配只能以英文或者汉字开头
                    Pattern p = Pattern.compile(nickMatcher);
                    Matcher m = p.matcher(input.charAt(0) + "");
                    if (!m.matches()) {
                        mEtNick.setText("");
                        ToastUtils.showToast(CodeUtils.getIdString(R.string.persion_nick_input));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    /**
     * 获取用户输入的昵称
     *
     * @return
     */
    public String getInputNick() {
        if (TextUtils.isEmpty(((EditText) get(R.id.et_changeNick_input)).getText().toString().trim())) {
            ToastUtils.showToast(CodeUtils.getIdString(R.string.string_hintInputRightNick));
            return "";
        }
        return ((EditText) get(R.id.et_changeNick_input)).getText().toString().trim();
    }
}
